/**
 * 
 */
package com.yst.metho.web.application.projectversion.actions.wbs;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.list.ListSelectionModel;

import com.yst.metho.web.application.components.listingview.IConfirmationAction;
import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.activities.Activity;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.rights.Right;
import com.yst.suivi.status.StatusConstants;

/**
 * @author perso
 * 
 */
public class CloseActivityAction implements IConfirmationAction
{
	private Activity mSelectedActivity = null;

	private IListingViewModel mModel;

	private Project mPrj;

	/**
	 * @param model
	 */
	public CloseActivityAction(IListingViewModel model, Project prj)
	{
		super();
		mModel = model;
		mPrj = prj;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#doAction()
	 */
	public void doAction()
	{
		try
		{
			Locator.getActivityView().closeActivity(mSelectedActivity.getId());
		}
		catch (EJBException e)
		{
			throw new RuntimeException(e);
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#getConfirmationMessageParameter()
	 */
	public String getConfirmationMessageParameter()
	{
		return "l'activite " + mSelectedActivity.getNom();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#setSelection(java.lang.Object)
	 */
	public void setSelection(Object source)
	{

		ListSelectionModel model = (ListSelectionModel) source;
		if (!model.isSelectionEmpty())
		{
			int selectedIndex = model.getMinSelectedIndex();
			mSelectedActivity = (Activity) mModel.getValueAt(0, selectedIndex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#isEnabled()
	 */
	public boolean isEnabled()
	{
		return StatusConstants.CLOSE != mPrj.getStatus() && StatusConstants.CLOSE != mSelectedActivity.getStatus() && StatusConstants.CLOSE != mSelectedActivity.getProjectVersion().getStatus();
	}

	public Right getRight()
	{
		return Right.CLOSE_ACTIVITY;
	}

}
